package com.think.teaching.controller;


import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.common.utils.Common;
import com.think.teaching.model.ExamTest;
import com.think.teaching.model.UserExamResult;
import com.think.teaching.service.impl.ExamTestServiceImpl;
import com.think.teaching.service.impl.UserExamResultServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户考试记录 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@RestController
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "用户考试结果管理接口")
public class UserExamResultController {
	
	private final UserExamResultServiceImpl userExamResultService;

	private final ExamTestServiceImpl examTestServiceImpl;
	
	public UserExamResultController(UserExamResultServiceImpl userExamResultService, ExamTestServiceImpl examTestServiceImpl) {
		this.userExamResultService = userExamResultService;
		this.examTestServiceImpl = examTestServiceImpl;
	}

	@ApiOperation("添加新的考试结果")
	@PostMapping("/user/userExamResult")
	public Result AddNewUserExamResult(@RequestBody()UserExamResult data) {
		Result result = data.CheckObjectComplete();
		if (result.getCode() == ResultCode.SUCCESS.code()) {
				if(userExamResultService.CheckDuplicate(data.getExamId(),Common.GetCurrentAuthor().getUserId()))
					return Result.failure(ResultCode.USER_EXAM_RESULT_ALREADY_EXIST);
			ExamTest test = examTestServiceImpl.GetExamTest(data.getExamId());
			if(test == null) {
				return Result.failure(ResultCode.EXAM_TEST_NOT_EXIST);
			}
			if(test.getStartTime().isAfter(LocalDateTime.now()) || test.getEndTime().isBefore(LocalDateTime.now())) {
				return Result.failure(ResultCode.EXAM_TEST_TIME_ERROR);
			}
			data.setStudentId(Common.GetCurrentAuthor().getUserId());
			UserExamResult userExamResult = userExamResultService.AddUserExamResult(data);
			if (userExamResult != null) {
				return Result.success();
			} else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		} else
			return result;
	}
	
	@ApiOperation("管理员删除考试结果")
	@RequestMapping(value = "admin/userExamResult/{id}",method = RequestMethod.DELETE)
	public Result DeleteUserExamResult(@PathVariable("id")Integer id) {
		UserExamResult userExamResult = userExamResultService.getById(id);
		if (userExamResult == null) {
			return Result.failure(ResultCode.USER_EXAM_RESULT_NOT_EXIST);
		} else {
			if (userExamResultService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("管理员获取考试结果详情")
	@RequestMapping(value = "admin/userExamResult",method = RequestMethod.GET)
	public Result GetUserExamResult(@RequestParam()Integer examId,
									@RequestParam()Integer studentId) {
		List<LinkedHashMap<String, Object>> userExamResult =
				userExamResultService.GetExamDetails(examId,studentId);
		if (userExamResult == null) {
			return Result.failure(ResultCode.USER_EXAM_RESULT_NOT_EXIST);
		} else {
			return Result.success(userExamResult);
		}
	}
	
	@ApiOperation("管理员修改考试结果状态")
	@RequestMapping(value = "admin/userExamResult/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateUserExamResultState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		UserExamResult userExamResult = userExamResultService.getById(id);
		if (userExamResult == null) {
			return Result.failure(ResultCode.USER_EXAM_RESULT_NOT_EXIST);
		} else {
			try {
				userExamResult = userExamResultService.SetUserExamResultState(id, state);
				return Result.success(userExamResult);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
	@ApiOperation("管理员搜索考试结果列表")
	@RequestMapping(value = "admin/userExamResults/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListUserExamResult(@PathVariable("page")Integer page,
                                          @PathVariable("count")Integer count,
                                          @RequestParam()Integer examId,
                                          @RequestParam(defaultValue = "2")Integer stateFilter) {
		return Result.success(userExamResultService.ListUserExamResult(page, count,examId,
                stateFilter));
	}
	
	@ApiOperation("用户搜索考试结果列表")
	@RequestMapping(value = "user/userExamResults/{page}/{count}/{id}",method = RequestMethod.GET)
	public Result UserListUserExamResult(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(userExamResultService.ListUserExamResultWithUserId(Common.GetCurrentAuthor().getUserId(), page, count, true));
	}
	@ApiOperation("老师批改试卷")
	@PostMapping("admin/userExamResults/correcting")
	public Result AdminCorresctingExam(@RequestBody UserExamResult data) {
		UserExamResult userExamResult = userExamResultService.getById(data.getId());
		if (userExamResult == null) {
			return Result.failure(ResultCode.USER_EXAM_RESULT_NOT_EXIST);
		} 
		else if (userExamResult.getCheckState() == 1) {
			return Result.failure(ResultCode.USER_EXAM_RESULT_ALREADY_CORRECTED);
		}
		else {
			userExamResult.setCheckState(1);
			userExamResult.setScore(data.getScore() == null?0:data.getScore());
			userExamResult.setUpdatedBy(Common.GetCurrentAuthor().getUserId());
			userExamResultService.updateById(userExamResult);
			return Result.success();
		}
	}



}

